package com.dhwl.hsh.driver.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.dhwl.hsh.driver.R;

/**
 * 带删除按钮的EditText
 * 
 * @author  Derek.Kidd
 * @version  [版本号, 2015-6-29]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class DeleteableEditText extends LinearLayout
{

    private ImageView icon;

    private EditText edt_input;
    
    private ImageButton btn_delete;
    
    private MyTextWatcher myTextWatcher;
    
    public DeleteableEditText(Context context)
    {
        super(context);
    }
    
    public DeleteableEditText(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        
        // 获取布局  
        View view = LayoutInflater.from(context).inflate(R.layout.layout_deletable_edit, this, true);
        icon = (ImageView)view.findViewById(R.id.icon_input);
        edt_input = (EditText)view.findViewById(R.id.edt_input);
        btn_delete = (ImageButton)this.findViewById(R.id.btn_delete);
        edt_input.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                // TODO Auto-generated method stub
            }
            
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {
                // TODO Auto-generated method stub
            }
            
            @Override
            public void afterTextChanged(Editable s)
            {
                btn_delete.setVisibility(edt_input.isFocused() && s.length() > 0 ? View.VISIBLE : View.GONE);
                if (null != myTextWatcher)
                {
                    myTextWatcher.afterTextChanged();
                }
            }
        });
        edt_input.setOnFocusChangeListener(new OnFocusChangeListener()
        {
            public void onFocusChange(View v, boolean hasFocus)
            {
                btn_delete.setVisibility(edt_input.getText().length() <= 0 || !hasFocus ? View.GONE : View.VISIBLE);
                if (hasFocus)
                {
                    edt_input.setSelection(edt_input.getText().length());
                }
            }
        });
        btn_delete.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                edt_input.setText("");
            }
        });
        
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeleteableEditText);
        icon.setImageResource(a.getResourceId(R.styleable.DeleteableEditText_icon_left,0));
        setText(a.getText(R.styleable.DeleteableEditText_text));
        setHint(a.getText(R.styleable.DeleteableEditText_hint));
        setTextColor(a.getResourceId(R.styleable.DeleteableEditText_textColor, Color.BLACK));
        setHintTextColor(a.getResourceId(R.styleable.DeleteableEditText_hintTextColor, Color.GRAY));
        edt_input.setInputType(a.getInt(R.styleable.DeleteableEditText_inputType, EditorInfo.TYPE_CLASS_TEXT));
        
        int maxlength = a.getInt(R.styleable.DeleteableEditText_maxLength, -1);
        if (maxlength >= 0)
        {
            edt_input.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxlength)});
        }
        else
        {
            edt_input.setFilters(new InputFilter[0]);
        }
        a.recycle();
    }
    
    public void setMyTextWatcher(MyTextWatcher myTextWatcher)
    {
        this.myTextWatcher = myTextWatcher;
    }
    
    /** 
     * @param str 设置输入框显示的默认值 
     */
    public void setHint(CharSequence str)
    {
        edt_input.setHint(str);
    }
    /**
     * @param resId 设置输入框显示的默认值
     */
    public void setHint(int resId)
    {
        edt_input.setHint(resId);
    }

    /** 
     * @return 获取输入框中的内容 
     */
    public String getText()
    {
        return edt_input.getText().toString().trim();
    }
    
    /** 
     * @param str 设置输入框内容 
     */
    public void setText(CharSequence str)
    {
        edt_input.setText(str);
    }
    
    /** 
     * @param id 字符id 
     */
    public void setText(int id)
    {
        edt_input.setText(id);
    }

    public void setTextColor(int resId){edt_input.setTextColor(resId);}

    public void setHintTextColor(int resId){edt_input.setHintTextColor(resId);}
    
    public EditText getEdt_input()
    {
        return edt_input;
    }
    
    public void setEdt_input(EditText edt_input)
    {
        this.edt_input = edt_input;
    }
    
    public ImageButton getBtn_delete()
    {
        return btn_delete;
    }
    
    public void setBtn_delete(ImageButton btn_delete)
    {
        this.btn_delete = btn_delete;
    }
    
    public interface MyTextWatcher
    {
        public void afterTextChanged();
    }
    
}
